#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    srand((size_t)time(nullptr));//利用时间戳当随机种子
    initUI();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::initUI()
{
    this->setWindowFlag(Qt::WindowType::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明
    //给窗口设置阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0,0);
    shadowEffect->setColor("#000000");
    shadowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shadowEffect);
    //设置左侧窗口按钮
    ui->Rec->setIcon(":/images/rec.png","推荐",0);
    ui->Music->setIcon(":/images/music.png","音乐馆",1);
    ui->audio->setIcon(":/images/radio.png","电台",2);
    ui->like->setIcon(":/images/like.png","我喜欢",3);
    ui->local->setIcon(":/images/local.png","本地音乐",4);
    ui->recent->setIcon(":/images/recent.png","最近播放",5);
    //绑定左侧按钮的槽函数
    conncetSignalAndSlot();
    //设置一个缺省页面，让推荐页面在运行后就以被点中出现
    defaultRec();
    //启动动画效果
    ui->like->showAnimation();
    RandomPicture();
    //为推荐页面添加图片
    ui->recMusicBox->initRecBoxUi(RandomPicture(),1);
    ui->supplyMusicBox->initRecBoxUi(RandomPicture(),2);
    //设置后我喜欢，本地音乐，最近播放的页面
    ui->likePage->setCommonPageUi("我喜欢",":/images/ilikebg.png");
    ui->localPage->setCommonPageUi("本地音乐",":/images/localbg.png");
    ui->recentPage->setCommonPageUi("最近播放",":/images/recentbg.png");
}

void Widget::conncetSignalAndSlot()
{
    connect(ui->Rec,&btForm::clicked,this,&Widget::onBtFromClicked);
    connect(ui->Music,&btForm::clicked,this,&Widget::onBtFromClicked);
    connect(ui->audio,&btForm::clicked,this,&Widget::onBtFromClicked);
    connect(ui->like,&btForm::clicked,this,&Widget::onBtFromClicked);
    connect(ui->local,&btForm::clicked,this,&Widget::onBtFromClicked);
    connect(ui->recent,&btForm::clicked,this,&Widget::onBtFromClicked);
}

void Widget::onBtFromClicked(int id)
{
    //利用findChildren函数获取当前页面所有btFrom按钮类型对象
    QList<btForm*> buttonList = this->findChildren<btForm*>();
    //遍历对象，把除当前id的效果都清除
    for(auto* bt:buttonList)
    {
        if(bt->getPageId() != id)
        {
            //清除效果
            bt->clearBg();
        }
    }
    ui->stackedWidget->setCurrentIndex(id);
}




void Widget::on_quit_clicked()
{
    this->close();
}

QJsonArray Widget::RandomPicture()
{
    QVector<QString> vecImageName;
    QDir dir(":/images/rec");
    QStringList files = dir.entryList(QDir::Files|QDir::NoDotAndDotDot);
    //输出文件名
    qDebug()<<"目录下的文件"<<endl;
    for(const QString& file:files)
        vecImageName.push_back(file);
    for(auto&x:vecImageName)
        qDebug()<<x<<endl;
    //因为每日推荐肯定都是随机的，所以我们需要把数组内容的元素给打散，利用随机函数random_shuffle,记得添加随机种子
    std::random_shuffle(vecImageName.begin(),vecImageName.end());
    //打散后，将其用Json序列化
    QJsonArray objArray;//存储Json对象
    for(int i = 0;i<vecImageName.size();++i)
    {
        QJsonObject obj;
        obj.insert("path",":/images/rec/"+vecImageName[i]);
        QString strText = QString("推荐-%1").arg(i,3,10,QChar('0'));
        obj.insert("text",strText);
        objArray.append(obj);
    }

    return objArray;
}

void Widget::mousePressEvent(QMouseEvent *event)
{
    //检测鼠标左键按下
    if(event->button() == Qt::LeftButton)
    {
        //获取窗口与鼠标的相对位置
        dragPosition = event->globalPos()-frameGeometry().topLeft();
        //qDebug()<<1<<endl;
        return;
    }
    //其他事件交给父类处理
    QWidget::mousePressEvent(event);

}

void Widget::mouseMoveEvent(QMouseEvent *event)
{
    //通过相对位置改变窗口位置
    if(event->buttons() == Qt::LeftButton)//event->buttons() 持续获取
    {
        move(event->globalPos()-dragPosition);
        return;
    }

    //其他事件交给父类处理
    QWidget::mouseMoveEvent(event);
}

void Widget::defaultRec()
{
    ui->Rec->changeColor();
}

